package api

import (
	"context"
	"gitee.com/fangy1111/shop/userop"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"userop_bff/global"
)

func Health(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "要不要看看我的妙妙屋",
		"data": nil,
	})
}

// 获取地址列表
func GetAddressList(c *gin.Context) {
	userId, _ := c.Get("uid")
	list, err := global.AddressSrvConn.GetAddressList(context.Background(), &userop.AddressRequest{
		UserId: userId.(int32),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "查询地址列表失败",
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": list,
	})
}

// 添加地址
func CreateAddress(c *gin.Context) {
	userId, _ := c.Get("uid")
	province := c.PostForm("province")
	city := c.PostForm("city")
	distRict := c.PostForm("district")
	address := c.PostForm("address")
	signerName := c.PostForm("signer_name")
	signerMobile := c.PostForm("signer_mobile")

	if len(province) == 0 || len(city) == 0 || len(distRict) == 0 || len(address) == 0 || len(signerName) == 0 || len(signerMobile) == 0 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "缺少参数",
			"data": nil,
		})
		return
	}

	_, err := global.AddressSrvConn.CreateAddress(context.Background(), &userop.AddressRequest{
		UserId:       userId.(int32),
		Province:     province,
		City:         city,
		District:     distRict,
		Address:      address,
		SignerName:   signerName,
		SignerMobile: signerMobile,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "添加地址失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "添加地址成功",
		"data": nil,
	})
}

// 删除地址
func DeleteAddress(c *gin.Context) {
	userId, _ := c.Get("uid")
	id := c.PostForm("id")
	idInt, _ := strconv.Atoi(id)
	if len(id) == 0 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "缺少参数",
			"data": nil,
		})
		return
	}
	_, err := global.AddressSrvConn.DeleteAddress(context.Background(), &userop.AddressRequest{
		Id:     int32(idInt),
		UserId: userId.(int32),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "删除地址",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "删除地址成功",
		"data": nil,
	})
}

// 修改地址
func UpdateAddress(c *gin.Context) {
	userId, _ := c.Get("uid")
	id := c.PostForm("id")
	idInt, _ := strconv.Atoi(id)

	province := c.PostForm("province")
	city := c.PostForm("city")
	distRict := c.PostForm("district")
	address := c.PostForm("address")
	signerName := c.PostForm("signer_name")
	signerMobile := c.PostForm("signer_mobile")
	if len(province) == 0 || len(city) == 0 || len(distRict) == 0 || len(address) == 0 || len(signerName) == 0 || len(signerMobile) == 0 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "缺少参数",
			"data": nil,
		})
		return
	}
	_, err := global.AddressSrvConn.UpdateAddress(context.Background(), &userop.AddressRequest{
		Id:           int32(idInt),
		UserId:       userId.(int32),
		Province:     province,
		City:         city,
		District:     distRict,
		Address:      address,
		SignerName:   signerName,
		SignerMobile: signerMobile,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "更新地址",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "更新地址成功",
		"data": nil,
	})
}
